package zhh.tangbao.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import zhh.tangbao.auxiliary.service.SystemFileService;
import zhh.tangbao.auxiliary.vo.SystemFileUploadVo;
import zhh.tangbao.system.annotation.Authority;
import zhh.tangbao.system.annotation.Log;
import zhh.tangbao.system.constant.AuthorityModel;
import zhh.tangbao.system.entity.PagerEntity;
import zhh.tangbao.system.result.Result;
import zhh.tangbao.system.utils.UserHolderUtil;
import zhh.tangbao.user.dto.*;
import zhh.tangbao.user.service.UserService;
import zhh.tangbao.user.vo.UserPageVo;
import zhh.tangbao.user.vo.UserVo;
import zhh.tangbao.wechat.dto.WeChatUserQueryDto;
import zhh.tangbao.wechat.entity.WeChatUser;
import zhh.tangbao.wechat.service.WeChatService;
import zhh.tangbao.wechat.vo.WeChatUserListOptionsVo;

import java.util.List;

/**
 * @author 粥灰灰
 * @version 1.0
 * @created 2022/8/20
 * @Description 描述
 */

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService service;

    @Autowired
    private WeChatService weChatService;

    @Autowired
    private UserHolderUtil userHolderUtil;

    @Autowired
    private SystemFileService fileService;

    @GetMapping("/getCurrentLoginUserInfo")
    @Authority
    public Result getUserInfo() {
        UserVo userVo = userHolderUtil.getCurrentUser();
        return Result.success().put(userVo);
    }

    @PostMapping("/login")
    @Authority(login = false)
    @Log(function = "登录", moduleName = "用户管理")
    public Result login(@RequestBody UserLoginDto dto) {
        // 登录成功后返回权限集合
        return Result.success().put(this.service.userLogin(dto));
    }

    @PostMapping("/add")
    @Authority(authorities = "user", authorityModel = AuthorityModel.CONTAIN)
    public Result add(@RequestBody UserDto dto) {
        return Result.successOrFail(service.addUser(dto));
    }

    @GetMapping("/userExist")
    @Authority(login = false)
    public Result userExist(String account) {
        return Result.success().put(service.userExist(account));
    }

    @DeleteMapping("/delete/{id}")
    @Authority(authorities = "user", authorityModel = AuthorityModel.CONTAIN)
    @Log(function = "删除用户", moduleName = "用户管理")
    public Result delete(@PathVariable long id) {
        return Result.successOrFail(service.deleteUser(id));
    }

    @PutMapping("/update")
    @Authority(authorities = "user", authorityModel = AuthorityModel.CONTAIN)
    public Result update(@RequestBody UserDto dto) {
        return Result.successOrFail(service.updateUser(dto));
    }

    @GetMapping("/logout")
    @Log(function = "登出", moduleName = "用户管理")
    public Result logout() {
        service.logout();
        return Result.success();
    }

    @PostMapping("/updatePassword")
    @Log(function = "修改密码", moduleName = "用户管理")
    @Authority(authorities = "user", authorityModel = AuthorityModel.CONTAIN)
    public Result updatePassword(@RequestBody UserPasswordDto dto) {
        return Result.successOrFail(service.updatePassword(dto));
    }

    @GetMapping("/addReplenishment")
    @Log(function = "添加补货人员权限", moduleName = "用户管理")
    @Authority(authorities = "user", authorityModel = AuthorityModel.CONTAIN)
    public Result addReplenishment(String id) {
        return Result.successOrFail(service.addReplenishment(id));
    }

    @GetMapping("/cancelReplenishment")
    @Log(function = "取消补货人员权限", moduleName = "用户管理")
    @Authority(authorities = "user", authorityModel = AuthorityModel.CONTAIN)
    public Result cancelReplenishment(String phone) {
        return Result.successOrFail(service.cancelReplenishment(phone));
    }

    @GetMapping("/wechatUserPage")
    @Authority(authorities = "user", authorityModel = AuthorityModel.CONTAIN)
    public Result wechatUserPage(WeChatUserDto dto) {
        return Result.success().put(service.wechatUserPage(dto));
    }

    /**
     * 用户是否在线
     */
    @GetMapping("/isOnline")
    @Authority
    public Result isOnline() {
        boolean isOnline = service.isOnline();
        return Result.success().put(isOnline);
    }

    @GetMapping("/page")
    @Authority(authorities = "user", authorityModel = AuthorityModel.ALL)
    public Result page(PagerEntity<UserPageVo> pager, UserDto dto) {
        PagerEntity<UserPageVo> result = service.page(pager, dto);
        return Result.success().put(result);
    }

    @PostMapping("/ban")
    @Authority(authorities = "user", authorityModel = AuthorityModel.ALL)
    @Log(function = "封禁用户", moduleName = "用户管理")
    public Result ban(@RequestBody UserBannedDto dto) {
        service.banUser(dto);
        return Result.success();
    }

    @PutMapping("/unBan")
    @Authority(authorities = "user", authorityModel = AuthorityModel.ALL)
    @Log(function = "解封用户", moduleName = "用户管理")
    public Result unBan(@RequestBody UserBannedDto dto) {
        service.unBanUser(dto);
        return Result.success();
    }

    @GetMapping("/check/{userAccount}")
    @Authority(login = false)
    public Result check(@PathVariable("userAccount") String userAccount) {
        return Result.success().put(service.userExist(userAccount));
    }

    @GetMapping("/getUserInfo")
    @Authority
    public Result getUserInfo(String id) {
        return Result.success().put(service.getUserDetailInfo(id));
    }

    @PostMapping("/uploadUserAvatar")
    @Authority
    public Result uploadUserAvatar(String userId, MultipartFile avatar) {
        SystemFileUploadVo upload = fileService.upload(avatar);
        service.updateUser(new UserDto().setId(userId).setUserAvatar(upload.getFileAccessPath()));
        return Result.success().put(upload);
    }

    @DeleteMapping("/batchDeleteUser")
    @Authority
    @Log(function = "删除用户", moduleName = "用户管理")
    public Result batchDeleteUser(@RequestBody List<String> ids) {
        int i = service.batchDeleteUser(ids);
        return Result.successOrFail(i);
    }

    @DeleteMapping("/batchDeleteReplenishment")
    @Authority(authorities = "user", authorityModel = AuthorityModel.CONTAIN)
    @Log(function = "移除管理员权限", moduleName = "用户管理")
    public Result batchDelete(@RequestBody List<String> ids) {
        return Result.successOrFail(weChatService.batchDeleteReplenishment(ids));
    }

    @GetMapping("/pageReplenishment")
    public Result pageReplenishment(WeChatUserQueryDto dto) {
        Page<WeChatUser> page = weChatService.page(dto);
        return Result.success().put(page);
    }


    @GetMapping("/userListOptions")
    public Result userListOptions(String queryString) {
        List<WeChatUserListOptionsVo> list = weChatService.userListOptions(queryString);
        return Result.success().put(list);
    }

    @GetMapping("/getWechatUserInfo")
    public Result getWechatUserInfo(String id) {
        WeChatUser info = weChatService.getWeChatUserById(id);
        return Result.success().put(info);
    }
}
